package cn.iocoder.yudao.module.product.controller.admin.cylinderinoutdetail;

import cn.iocoder.yudao.module.product.controller.admin.cylinderinoutdetail.vo.CylinderInOutDetailSaveReqVO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.product.controller.admin.cylinderinoutdetail.vo.*;
import cn.iocoder.yudao.module.product.dal.dataobject.cylinderinoutdetail.CylinderInOutDetailDO;
import cn.iocoder.yudao.module.product.service.cylinderinoutdetail.CylinderInOutDetailService;

@Tag(name = "管理后台 - 钢瓶出入库详情")
@RestController
@RequestMapping("/product/cylinder-in-out-detail")
@Validated
public class CylinderInOutDetailController {

    @Resource
    private CylinderInOutDetailService cylinderInOutDetailService;

    @PostMapping("/create")
    @Operation(summary = "创建钢瓶出入库详情")
    @PreAuthorize("@ss.hasPermission('product:cylinder-in-out-detail:create')")
    public CommonResult<Long> createCylinderInOutDetail(@Valid @RequestBody CylinderInOutDetailSaveReqVO createReqVO) {
        return success(cylinderInOutDetailService.createCylinderInOutDetail(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新钢瓶出入库详情")
    @PreAuthorize("@ss.hasPermission('product:cylinder-in-out-detail:update')")
    public CommonResult<Boolean> updateCylinderInOutDetail(@Valid @RequestBody CylinderInOutDetailSaveReqVO updateReqVO) {
        cylinderInOutDetailService.updateCylinderInOutDetail(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除钢瓶出入库详情")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('product:cylinder-in-out-detail:delete')")
    public CommonResult<Boolean> deleteCylinderInOutDetail(@RequestParam("id") Long id) {
        cylinderInOutDetailService.deleteCylinderInOutDetail(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得钢瓶出入库详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('product:cylinder-in-out-detail:query')")
    public CommonResult<CylinderInOutDetailRespVO> getCylinderInOutDetail(@RequestParam("id") Long id) {
        CylinderInOutDetailDO cylinderInOutDetail = cylinderInOutDetailService.getCylinderInOutDetail(id);
        return success(BeanUtils.toBean(cylinderInOutDetail, CylinderInOutDetailRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得钢瓶出入库详情分页")
    @PreAuthorize("@ss.hasPermission('product:cylinder-in-out-detail:query')")
    public CommonResult<PageResult<CylinderInOutDetailRespVO>> getCylinderInOutDetailPage(@Valid CylinderInOutDetailPageReqVO pageReqVO) {
        PageResult<CylinderInOutDetailDO> pageResult = cylinderInOutDetailService.getCylinderInOutDetailPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, CylinderInOutDetailRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出钢瓶出入库详情 Excel")
    @PreAuthorize("@ss.hasPermission('product:cylinder-in-out-detail:export')")
    @OperateLog(type = EXPORT)
    public void exportCylinderInOutDetailExcel(@Valid CylinderInOutDetailPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<CylinderInOutDetailDO> list = cylinderInOutDetailService.getCylinderInOutDetailPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "钢瓶出入库详情.xls", "数据", CylinderInOutDetailRespVO.class,
                        BeanUtils.toBean(list, CylinderInOutDetailRespVO.class));
    }

}